<template>
  <div class="news">
    <div class="title">
      <h1>房屋咨询一手知</h1>
      <p style="font-size: large">最新资讯，畅快掌握</p>
    </div>
    <div class="msg">
      <div class="type">
        <el-select v-model="news.title" placeholder="请输入你想查找的资讯类型">
            <el-option label="房屋资讯" value="房屋资讯"></el-option>
            <el-option label="系统公告" value="系统公告"></el-option>
        </el-select>
        <el-button type="primary" @click="getNewsList">搜索</el-button>
      </div>
      <div v-for="item in currentList" :key="item.id" class="info">
        <div class="content">
          <h2>{{item.title}}</h2>
          <p>{{item.content}}</p>
          <p>发布时间：<span>{{item.createTime}}</span></p>
        </div>
        <div class="actions">
          <el-button
              :class="{ 'is-liked': likes[item.id] }"
              type="text"
              @click="handleLike(item)"
          >
          <i>点赞({{item.liked}})</i>
          </el-button>
          <el-button
              :class="{ 'is-favorited': favorites[item.id] }"
              type="text"
              @click="handleFavorite(item)"
          >
          <i>收藏({{item.collected}})</i>
          </el-button>
        </div>
      </div>
    </div>
    <div class="page">
      <el-pagination
          :current-page.sync="pageNum"
          :page-size="pageSize"
          :page-sizes="[5,10,15,20]"
          :total="allList.length"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handlePage"
          >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

const user = JSON.parse(localStorage.getItem("login-user"));

export default {
  name: "News",
  mounted() {
    this.getNewsList();
  },
  data() {
    return {
      list:[],
      news:{
        title:'',
      },
      allList:[],
      pageNum: 1, // 当前页码
      pageSize: 5,// 每页显示的租房信息数量
      currentList:[],
      totalData:[],
      total:0,
      likes: {},
      favorites: {},
      // liked: 0,
      // collected: 0,
    };
  },
  methods: {
    getNewsList(){
      axios({
        url: "http://localhost:8090/news/list",
        method: 'post',
        data: {
          title:this.news.title
        }
      }).then(res => {
        if(res.code === '200'){
          this.allList = res.data;
          this.handlePage(1);
        }
      })
    },

    handleSizeChange(size) {
      this.pageSize = size;
      this.handlePage(1,size)
    },
    //分页
    handlePage(page) {
      const start = (page - 1) * this.pageSize;
      const end = page * this.pageSize;
      this.currentList = this.allList.slice(start, end);
    },

    //点赞逻辑
    handleLike(item) {
      if (!user){
        this.$message.warning('登录后才能点赞！');
        return;
      }
      // 处理点赞逻辑
      this.$set(this.likes, item.id, !this.likes[item.id]);
      //增加点赞数量
      axios({
        url:'/news/addLiked',
        method:'put',
        data:{
          id:item.id
        }
      }).then(res=>{
        if (res.code === '200'){
          this.getNewsList()
        }else{
          this.$message.error(res.msg)
        }
      })
    },

    //处理收藏逻辑
    handleFavorite(item) {
      if (!user){
        this.$message.warning('登录后才能收藏！');
        return;
      }
      // 处理收藏逻辑
      this.$set(this.favorites, item.id, !this.favorites[item.id]);

      //收藏数量增加
      axios({
        url:'/news/addCollected',
        method:'put',
        data:{
          id:item.id
        }
      }).then(res=>{
        if (res.code === '200'){
          this.getNewsList()
        }else{
          this.$message.error(res.msg)
        }
      })
    },
  },
}
</script>

<style scoped>
.news{
  min-height: 830px ;
  background-color: white;
}
a{
  text-decoration: none;
  color: black;
}

.title{
  height: 300px;
  padding: 50px 500px 10px;
  background-image: url("@/assets/background6.jpg");
  color: #fa5d03;
}

.title h1{
  font-size: 40px;
}

.msg {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 0;
}

.type{
  display: flex;
  margin-top: 5px;
}

.info{
  width: 850px;
  height: 160px;
  background-color: whitesmoke;
  padding: 10px 20px;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  display: flex;
}

.content{
  flex: 1;
}

.actions{
  display: flex;
  gap: 2px;
}

.page{
  display: flex;
  justify-content: center;
  padding-bottom: 35px;
}

.is-liked {
  /*background-color: #409EFF !important; !* 点赞按钮的背景颜色 *!*/
  color: #c21919 !important;
}

.is-favorited {
  /*background-color: #E6A23C !important;*/ /* 收藏按钮的背景颜色 */
  color: #c21919 !important;
}
</style>